package com.example.basicknowledge.菜单的使用;

import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;

import com.example.basicknowledge.R;

public class UsageOfMenu extends AppCompatActivity {
    TextView textView;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.usage_of_menu);
        textView = (TextView)findViewById(R.id.textView7);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        super.onCreateOptionsMenu(menu);
        //四个参数的含义。1，group的id,2,item的id,3,是否排序，4，将要显示的内容
        menu.add(0,Menu.FIRST,0,"red");
        menu.add(0,Menu.FIRST + 1,0,"green");
        menu.add(0,Menu.FIRST + 2,0,"blue");
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        System.out.println(item.getItemId());
        switch (item.getItemId()) {
            case Menu.FIRST :
                textView.setBackgroundColor(Color.RED);
                break;
            case Menu.FIRST + 1 :
                textView.setBackgroundColor(Color.GREEN);
                break;
            case Menu.FIRST + 2 :
                textView.setBackgroundColor(Color.BLUE);
                break;
        }
        return super.onOptionsItemSelected(item);
    }
}
